package net.hcpme.dao;

import java.sql.SQLException;

import net.hcpme.db.DatabaseHelper;
import android.content.Context;

import com.j256.ormlite.android.apptools.OpenHelperManager;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;

public class MessageDao {

    private Dao<Message, Integer> dao = null;
    private Context context = null;
    private OrmLiteSqliteOpenHelper helper = null;

    public MessageDao(Context context) {
        this.context = context;
        helper = OpenHelperManager.getHelper(this.context, DatabaseHelper.class);
        try {
            dao = helper.getDao(Message.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void releaseHelper() {
        if (helper != null) {
            OpenHelperManager.releaseHelper();
            helper = null;
        }
    }

    public boolean installMessage(Message message) {
        try {
            int index = dao.create(message);
            if (index != 1) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

}
